<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @author Walter Tamboer
 * @package Framework03
 */

/**
 * A select statement selects data from the database table.
 */
class FW_Database_Statement_Select extends FW_Database_Statement_Abstract
{
    /**
     * @var array A list with columns to select from the database table.
     */
    private $columns = array();

    /**
     * @var array A list with where statements. column => array(value, operator)
     */
    private $where = array();

    /**
     * @var array A list with columns to order.
     */
    private $order = array();

    /**
     * @var int The amount of rows to limit the statement on.
     */
    private $limitAmount = 0;

    /**
     * @var int The row to start the limiting from.
     */
    private $limitRow = 0;

    /**
     * Initializes a new instance of this class.
     * @param FW_Database_Table $table The table to use in this statement.
     * @param array $columns The optional columns that should be selected.
     */
    public function __construct(FW_Database_Table $table, array $columns = array())
    {
        $this->setTableName($table);
        
        $this->columns = $columns;
    }
    
    /**
     * Adds a where statement where $name is equal to $value.
     * @param string $name
     * @param string $value
     * @return FW_Database_Statement_Select
     */
    public function where($name, $value)
    {
        $this->where[$name] = array($value, '=');
        return $this;
    }

    /**
     * Gets the array with the where columns.
     * @return array
     */
    public function getWhere()
    {
        return $this->where;
    }
    
    /**
     * Adds an order statement to this select statement.
     * @param string $column The name of the column to sort.
     * @param bool $ascending Whether or not to sort the column ascending.
     * @return FW_Database_Statement_Select
     */
    public function order($column, $ascending = true)
    {
        $this->order[$column] = $ascending;
        return $this;
    }

    /**
     * Gets the array with the columns to order by.
     * @return array
     */
    public function getOrder()
    {
        return $this->order;
    }
    
    /**
     * Limits the results of this query by the given amount and from the given row.
     * @param int $amount The amount to limit the results with.
     * @param int $row The row to start from.
     * @return FW_Database_Statement_Select
     */
    public function limit($amount, $row = 0)
    {
        $this->limitAmount = $amount;
        $this->limitRow = $row;
        return $this;
    }

    /**
     * Gets the amount of rows for the LIMIT statement.
     * @return int
     */
    public function getLimitAmount()
    {
        return $this->limitAmount;
    }

    /**
     * Gets the starting row for the LIMIT statement.
     * @return int
     */
    public function getLimitRow()
    {
        return $this->limitRow;
    }

    /**
     * Gets the columns that should be selected from the database table.
     * @return array
     */
    public function getColumns()
    {
        return $this->columns;
    }
}
